<template>
  <div class="category">
    <h1>{{id?'编辑':"新增"}}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs v-model="active" type="border-card">
        <el-tab-pane label="基础信息" name="baseInfo">
          <el-form-item label="名称:">
            <el-input v-model="form.name"></el-input>
          </el-form-item>

          <el-form-item label="头像:">
            <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeads()" :show-file-list="false" :on-success="afterupload">
              <img v-if="form.avatar" :src="form.avatar" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="称号:">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="分类：">
            <el-select v-model="form.categories" multiple>
              <el-option v-for="item of categories" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度：">
            <el-rate v-model="form.scores.difficult" :max="9" style="margin-top: 0.6rem;">
            </el-rate>
          </el-form-item>
          <el-form-item label="技能：">
            <el-rate v-model="form.scores.skills" :max="9" style="margin-top: 0.6rem;">
            </el-rate>
          </el-form-item>
          <el-form-item label="攻击：">
            <el-rate v-model="form.scores.attack" :max="9" style="margin-top: 0.6rem;">
            </el-rate>
          </el-form-item>
          <el-form-item label="生存：">
            <el-rate v-model="form.scores.survive" :max="9" style="margin-top: 0.6rem;">
            </el-rate>
          </el-form-item>

          <el-form-item label="顺风出装：">
            <el-select v-model="form.goods1" multiple>
              <el-option v-for="item of goods" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装：">
            <el-select v-model="form.goods2" multiple>
              <el-option v-for="item of goods" :key="item._id" :label="item.name" :value="item._id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧:">
            <el-input v-model="form.usageTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="对战技巧:">
            <el-input v-model="form.battleTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="团战技巧:">
            <el-input v-model="form.teamTips" type="textarea"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane name="skills" label="技能">
          <el-button size="small" @click="form.skills.push({})"><i class="el-icon-plus"></i> 添加技能</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,index) in form.skills" :key="index">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeads()" :show-file-list="false" :on-success="res=>$set(item,'icon',res.url)">
                  <img v-if="item.icon" :src="item.icon" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-form-item label="提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="danger" size="small" @click="form.skills.splice(i,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>

        </el-tab-pane>
      </el-tabs>

      <el-form-item>
        <el-button type="primary" style="margin-top:1rem" native-type="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import http from "../axios/index.js";
export default {
  props: {
    id: String,
  },
  data() {
    return {
      form: {
        avatar: null,
        name: null,
        scores: {
          difficult: 0,
        },
        skills: [],
      },
      parents: [],
      categories: [],
      goods: [],
      active: "skills",
    };
  },
  created() {
    this.id && this.fetch();
    this.fetchParent();
    this.fetchCategory();
    this.fetchGoods();
  },
  methods: {
    // 图片上传之后
    afterupload(res) {
      this.$set(this.form, "avatar", res.url);
      // this.form.icon = res.url;
    },
    // 获取
    async fetch() {
      const res = await this.$http.get(`/rest/heroes/${this.id}`);
      this.form = res.data;
    },
    async fetchParent() {
      const res = await this.$http.get(`/rest/heroes`);
      this.parents = res.data;
    },
    // 获取分类
    async fetchCategory() {
      const res = await this.$http.get(`/rest/categories`);
      this.categories = res.data;
    },
    // 获取出装
    async fetchGoods() {
      const res = await this.$http.get(`/rest/goods`);
      this.goods = res.data;
    },
    async save() {
      let res;
      if (this.id) {
        res = await this.$http.put(`/rest/heroes/${this.id}`, this.form);
      } else {
        res = await this.$http.post("/rest/heroes", this.form);
      }
      const { data } = res;
      if (data.code == 200) {
        this.$message.success(data.msg);
        this.$router.push("/heroes/list");
      }
    },
  },
};
</script>

<style>
</style>
